package utils

import (
	"io/ioutil"
	"mynetdisk/controller"
	"os"
	"path/filepath"
	"strings"
)

/**
 *@Author tudou
 *@Date 2019/8/26
 **/

func GetDirList(userId uint)([]string,error){
	rePath:=controller.StaticPath+ParseUintToString(userId)
	var dir_list []string
	dir_err:=filepath.Walk(rePath,
		func(path string, info os.FileInfo, err error) error {
			if info==nil{
				return err
			}
			if info.IsDir(){
				dir_list=append(dir_list,path)
				return nil
			}
			return nil
		})
	if dir_err!=nil{
		return nil,dir_err
	}
	return dir_list[1:],dir_err
}

func DirExisted(finalPath string,userId uint)bool{
	paths,err:=GetDirList(userId)
	if err!=nil{
		return false
	}
	finalPath=strings.ReplaceAll(finalPath,"/","\\")
	for _,v:=range paths{
		if v==finalPath{
			return true
		}
	}
	return false
}

func FileExisted(dir string,fileName string)bool{
	files,err:=ioutil.ReadDir(dir)
	if err!=nil{
		return false
	}
	for _,v:=range files{
		if v.IsDir(){
			continue
		}
		if v.Name()==fileName{
			return true
		}
	}
	return false
}